package db;

import java.io.*;
import java.sql.Timestamp;
import java.util.*;

public class LogDat {
	private static LogDat instance = null; //Singleton
	static FileWriter fwr = null;
	static BufferedWriter bwr = null;
	static ArrayList<String> logs = null;
	
	private LogDat(){
		logs = new ArrayList<String>();
	}
	
	public synchronized static LogDat getInstance() 
    {
        if (instance == null) 
        {
            instance = new LogDat();
        }
        return instance;
    }
	
	public void addLog(String log){
		Calendar calendar = Calendar.getInstance();
		Timestamp tstamp = new Timestamp(calendar.getTimeInMillis());
		logs.add(tstamp.toString()+ "  " + log);
	}
	
	public void close() throws IOException{
		fwr = new FileWriter("DB2Parser.log");
		bwr = new BufferedWriter(fwr, 1024);
		
		for(Iterator<String> log = logs.iterator();log.hasNext(); ){
			String outputline = log.next();
			bwr.write(outputline,0,outputline.length());
			bwr.newLine();
			}
		bwr.close();
	}
}
